//
//  LoadAnimation.m
//  LoadImageAnimation
//
//  Created by Burn on 2019/3/18.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "LoadAnimation.h"

@interface LoadAnimation ()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) NSMutableArray *imageArray;

@end
@implementation LoadAnimation





- (void)showInView:(UIView *)view
{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *nav = [tabbarController.childViewControllers firstObject];
    CGRect statuF = [UIApplication sharedApplication].statusBarFrame;
    CGRect navF = nav.navigationBar.frame;
    
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.backgroundColor = [UIColor clearColor];
    CGSize size = [[UIImage imageNamed:@"Loading_7@2x.tiff"] size];
    self.imageView.frame = CGRectMake(0, 0, size.width, size.height);
    self.imageView.center = CGPointMake(self.center.x,self.center.y - (navF.size.height + statuF.size.height) * 0.5);
    [self addSubview:self.imageView];
    [view addSubview:self];
    [self.imageView startAnimating];
}

- (void)dismiss
{
    [self.imageView stopAnimating];
    [self.imageArray removeAllObjects];
    [self.imageView removeFromSuperview];
    [self removeFromSuperview];
}



- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
        _imageView.animationDuration = 0.5;
        _imageView.animationRepeatCount = 0;
        _imageView.animationImages = self.imageArray;
    }
    return _imageView;
}


- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
        for (int i = 1; i <= 8; i++) {
            UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"Loading_%d@2x.tiff",i]];
            [_imageArray addObject:image];
        }
    }
    return _imageArray;
}

@end
